package persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import negocio.Area;
import negocio.Curso;
import negocio.Instructor;
import negocio.Sala;

public class SalaDAO
{
	private static SalaDAO instancia;
	private final Connection c;
	
	private SalaDAO()
	{
		c = BDManager.getInstance().getConexion();
	}

	public static SalaDAO getInstance()
	{
		if(instancia ==null)
		{
			instancia = new SalaDAO();
		}
		return instancia;
	}

	public Sala getSala(int id) 
	{
		String query = "SELECT * FROM sala WHERE sala_id="+id;
		Statement s;
		ResultSet rs;
		Sala res = null;
		try{
			s = c.createStatement();
			rs = s.executeQuery(query);
			rs.next();
			res = new Sala(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getInt(4));
			s.close();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		return res;
	}

	public ArrayList<Sala> getAll() 
	{
		String query = "SELECT * FROM sala";
		Statement s;
		ResultSet rs;
		ArrayList<Sala> res = new ArrayList<Sala>();
		try {
			s = c.createStatement();
			rs = s.executeQuery(query);
			while(rs.next())
			{
				res.add(new Sala(rs.getInt("sala_id"), rs.getInt("numero"), rs.getInt("capacidadmaxima"), rs.getInt("cantidadpcs")));
			}
			s.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return res;
	}
}
